import json
import re
from bs4 import BeautifulSoup
import requests
import time

from mysql.mysqlconnect import MysqlConnect
from common.urlencode import UrlEncode
from entitys import *


# 天气预报从http://weathernew.pae.baidu.com此网页获取， get请求，但是需要城市的urlEncode编码
# urlEncode编码使用https://tool.lu/encdec/这个网页来获取，post请求

class MyWeatherNews:
    # 获取天气情况url，其中query为目标城市unicode，city_name，province_name不需要为目标城市
    # 这个url理论上只要提供区县级区划名就能查到天气。为了准确还是带上省份
    url = 'http://weathernew.pae.baidu.com/weathernew/pc?' \
          'query={0}{1}%E5%A4%A9%E6%B0%94&' \
          'srcid=4982&' \
          'city_name={1}' \
          'province_name={0}'

    selectors = 'body > div#sfr-app > div.rt-view > div.rt-body > script:nth-child(5)'

    def __init__(self, pro, city):
        self.url = self.url.format(pro, city)

    def getWeatherNews(self):
        response = requests.get(self.url)
        soup = BeautifulSoup(response.text, 'lxml')
        # 拿到数据
        data = str(soup.select(self.selectors)[0])

        todaymap = {}
        todayweather = {}
        # keys列表，为后续拓展提供可能，目前只获取weather数据
        keys = ['weather', 'psPm25']
        for key in keys:
            # 根据key拿到指定的数据
            # 使用正则的()分组，可以过滤掉前面的data["xxx"]，只留下数据，转json更方便
            pattern = re.compile(r'data\["' + key + '"]=({[^}]+})', re.MULTILINE | re.DOTALL)
            destdata = pattern.findall(data)
            map = json.loads(destdata[0])
            if 'weather' == key:
                todaymap['weather'] = map
                todayweather = map
                print(map)
                print('-->今日最高温度: ' + map['temperature_day'])
                print('-->当前温度: ' + map['temperature'])
                print('-->今日最低温度: ' + map['temperature_night'])
            elif 'psPm25' == key:
                todaymap['psPm25'] = map
                psPm25 = map
                print(psPm25)
                print('-->pm2.5: ' + map['ps_pm25'])
                print('-->空气质量: ' + map['level'])
        return todaymap


if __name__ == '__main__':
    urlencoder = UrlEncode()
    province = '福建'
    city = '福州'
    provinceUnicode = urlencoder.urlEncode(province)
    cityUnicode = urlencoder.urlEncode(city)

    myweathernews = MyWeatherNews(provinceUnicode, cityUnicode)
    todaymap = myweathernews.getWeatherNews()
    todayweather = todaymap['weather']
    psPm25 = todaymap['psPm25']

    sqlconnect = MysqlConnect()

    # 先拿到城市id
    citysql = f'select id from district where district_name = "{city}" and id like concat((select id from district where district_name = "{province}" and length(id) = 3),"%")'
    cityid = sqlconnect.select(citysql)[0]['id']
    # 拿到日期字段
    date = time.strftime('%y%m%d', time.localtime())

    todayweather.update({'city_id': cityid, 'date': date})
    rowcount = sqlconnect.replace(todayweather, Weather())
    if rowcount != 'error':
        print('影响weather表' + str(rowcount) + '条数据')

    pspmlist = ['优', '良']
    psPm25.update({'city_id': cityid, 'date': date, 'level_num': pspmlist.index(psPm25['level'])})
    rowcount = sqlconnect.replace(psPm25, PsPm25())
    if rowcount != 'error':
        print('影响ps_pm25表' + str(rowcount) + '条数据')

    sqlconnect.__close__()
